/**
 * Created by Administrator on 2017/5/17.
 最新最全最好的Bootstrap模板：http://www.bootstrapmb.com
 */

$(function(){
    // index();
    $(".index_nav ul li").each(function(index){
        $(this).click(function(){
            $(this).addClass("nav_active").siblings().removeClass("nav_active");
            $(".index_tabs .inner").eq(index).fadeIn().siblings("div").stop().hide();
            if(index==1){
                yingXiao();
            }else if(index==2){
                shouRu();
            }else if(index==3){
                AnQuan();
            }else if(index==4){
                user();
            }else if(index==5){
                manage();
            }
        })
    });
    $(".tabs ul li").each(function(index){
       $(this).click(function(){
           $(".tabs ul li div .div").removeClass("tabs_active");
           $(this).find("div .div").addClass("tabs_active");
           $(".tabs_map>div").eq(index).fadeIn().siblings("div").stop().hide();
       })
   });
    $(".middle_top_bot ul li").each(function(){
        $(this).click(function(){
            $(".middle_top_bot ul li").removeClass("middle_top_bot_active");
            $(this).addClass("middle_top_bot_active");
        })
    });

});


(function() {
	
	var websocket = null;
	if('WebSocket' in window){
		websocket = new WebSocket("ws://127.0.0.1:8181/logic/ws/DanmakuCount");
	}else{
		alert('Not support websocket')
	}
	
	websocket.onerror = function(){
		setMessageInnerHTML("error");
	};
	
	websocket.onopen = function(event){
		setMessageInnerHTML("open");
	}
	
	websocket.onmessage = function(event){
		var result = JSON.parse(event.data);
		setMessageInnerHTML(result);
	}
	
	websocket.onclose = function(){
		setMessageInnerHTML("close");
	}
	
	window.onbeforeunload = function(){
		websocket.close();
	}
	
	function setMessageInnerHTML(data){
		var myChart = echarts.init(document.querySelector('.ciyun'));
		// console.log(data);
		var contents = new Array();
		var timess = new Array();
		var cnt = 0;
		// console.log(data);
		for (var i in data) {
			contents[i] = data[i].content;
			timess[i] = data[i].times;
			cnt ++;
			if (cnt > 10)	break;
		}
		// console.log(contents);
		
		myChart.setOption({
			
			textStyle: {
				color: '#FFF',
			},
			title: {
				text: '热门榜弹幕频度统计表',
				textStyle: {
					color: '#FFF',
				},
			},
			tooltip: {
				trigger: 'axis',
				axisPointer: {
					type: 'shadow'
				}
			},
			xAxis: {
			    type: 'category',
			    data: contents,
				axisLabel: {
					interval: 0,
					rotate: 30
				},
			  },
			  yAxis: {
			    type: 'value'
			  },
			  series: [
			    {
			      data: timess,
			      type: 'bar',
			      showBackground: true,
			      backgroundStyle: {
			        color: 'rgba(180, 180, 180, 0.2)'
			      }
			    }
			  ]
		})
		// showEcharts(myChart, data);
	}
	
	function closeWebSocket(){
		websocket.close();
	}
	
	function send(){
		var message = document.getElementById('text').value;
		websocket.send(message);
	}
})();


// 主页面右侧热门榜对接
(function() {

	myUser = {
		"id": 11
	}

	var pics = new Array();
	var titles = new Array();
	var bvids = new Array();

	$.ajax({
		contentType: "application/json;charset=utf-8",
		type: "put",
		url: "http://localhost:8181/hotList/getAllHotList",
		data: JSON.stringify(myUser),
		dataType: "json",
		success: function(res) {
			if (res) {
				// console.log(res);
				for (var i in res) {
					pics[i] = res[i].pic;
					titles[i] = res[i].title;
					bvids[i] = res[i].bvid;
					if (i == 49) {
						break;
					}
				}
				var rlist = document.getElementById("rlist");
				var lis = rlist.getElementsByTagName("li");
				// console.log(lis);
				for (var i in lis) {
					var img = lis[i].getElementsByClassName("img_pic")[0];
					i = parseInt(i);
					img.setAttribute("src", pics[i]);
					var span = lis[i].getElementsByTagName("span")[0];
					span.innerHTML = titles[i];
					var a = lis[i].getElementsByTagName("a")[0];
					a.setAttribute("href", "./comment/index.html?bvid="+bvids[i]);
					var a = lis[i].getElementsByTagName("a")[1];
					a.setAttribute("href", "./comment/index.html?bvid="+bvids[i]);
					if (i == 9) break;
				}
			}
		},
		error: function(errorMag) {
			alert("加载失败");
			myChart.hideLoading();
		}
	});

})();

// 主页面左侧全站信息
(function() {

	myUser = {
		"id": 11
	}

	var pics = new Array();
	var titles = new Array();

	$.ajax({
		contentType: "application/json;charset=utf-8",
		type: "put",
		url: "http://localhost:8181/hotList/getAllAllList",
		data: JSON.stringify(myUser),
		dataType: "json",
		success: function(res) {
			if (res) {
				// console.log(res);
				for (var i in res) {
					pics[i] = res[i].pic;
					titles[i] = res[i].title;
					if (i == 49) {
						break;
					}
				}

				var llist = document.getElementById("llist");
				var lis = llist.getElementsByTagName("li");

				for (var i in lis) {
					var img = lis[i].getElementsByClassName("img_pic")[0];
					i = parseInt(i);
					img.setAttribute("src", pics[i]);
					var span = lis[i].getElementsByTagName("span")[0];
					span.innerHTML = titles[i];
					if (i == 9) break;
				}

			}

			var btn1 = document.getElementById("btn1");
			btn1.onclick = function() {
				var llist = document.getElementById("llist");
				var lis = llist.getElementsByTagName("li");

				for (var i in lis) {
					var img = lis[i].getElementsByClassName("img_pic")[0];
					i = parseInt(i);
					img.setAttribute("src", pics[i]);
					var span = lis[i].getElementsByTagName("span")[0];
					span.innerHTML = titles[i];
					if (i == 9) break;
				}
			}
		},
		error: function(errorMag) {
			alert("加载失败");
			myChart.hideLoading();
		}
	});

})();


//主页面左侧动画信息
(function() {

	myUser = {
		"id": 11
	}

	var pics = new Array();
	var titles = new Array();

	$.ajax({
		contentType: "application/json;charset=utf-8",
		type: "put",
		url: "http://localhost:8181/hotList/getAllDougaList",
		data: JSON.stringify(myUser),
		dataType: "json",
		success: function(res) {
			if (res) {
				// console.log(res);
				for (var i in res) {
					pics[i] = res[i].pic;
					titles[i] = res[i].title;
					if (i == 49) {
						break;
					}
				}

				var btn2 = document.getElementById("btn2");
				btn2.onclick = function() {
					var llist = document.getElementById("llist");
					var lis = llist.getElementsByTagName("li");
					console.log(pics[10]);

					for (var i in lis) {
						var img = lis[i].getElementsByClassName("img_pic")[0];
						i = parseInt(i);
						img.setAttribute("src", pics[i]);
						var span = lis[i].getElementsByTagName("span")[0];
						span.innerHTML = titles[i];
						if (i == 9) break;
					}
				}
			}
		},
		error: function(errorMag) {
			alert("加载失败");
			myChart.hideLoading();
		}
	});

})();


//主页面左侧舞蹈信息
(function() {

	myUser = {
		"id": 11
	}

	var pics = new Array();
	var titles = new Array();

	$.ajax({
		contentType: "application/json;charset=utf-8",
		type: "put",
		url: "http://localhost:8181/hotList/getAllDanceList",
		data: JSON.stringify(myUser),
		dataType: "json",
		success: function(res) {
			if (res) {
				// console.log(res);
				for (var i in res) {
					pics[i] = res[i].pic;
					titles[i] = res[i].title;
					if (i == 49) {
						break;
					}
				}

				var btn3 = document.getElementById("btn3");
				btn3.onclick = function() {
					var llist = document.getElementById("llist");
					var lis = llist.getElementsByTagName("li");
					console.log(pics[10]);

					for (var i in lis) {
						var img = lis[i].getElementsByClassName("img_pic")[0];
						i = parseInt(i);
						img.setAttribute("src", pics[i]);
						var span = lis[i].getElementsByTagName("span")[0];
						span.innerHTML = titles[i];
						if (i == 9) break;
					}
				}
			}
		},
		error: function(errorMag) {
			alert("加载失败");
			myChart.hideLoading();
		}
	});

})();

//主页面左侧游戏信息
(function() {

	myUser = {
		"id": 11
	}

	var pics = new Array();
	var titles = new Array();

	$.ajax({
		contentType: "application/json;charset=utf-8",
		type: "put",
		url: "http://localhost:8181/hotList/getAllGameList",
		data: JSON.stringify(myUser),
		dataType: "json",
		success: function(res) {
			if (res) {
				// console.log(res);
				for (var i in res) {
					pics[i] = res[i].pic;
					titles[i] = res[i].title;
					if (i == 49) {
						break;
					}
				}

				var btn4 = document.getElementById("btn4");
				btn4.onclick = function() {
					var llist = document.getElementById("llist");
					var lis = llist.getElementsByTagName("li");
					console.log(pics[10]);

					for (var i in lis) {
						var img = lis[i].getElementsByClassName("img_pic")[0];
						i = parseInt(i);
						img.setAttribute("src", pics[i]);
						var span = lis[i].getElementsByTagName("span")[0];
						span.innerHTML = titles[i];
						if (i == 9) break;
					}
				}
			}
		},
		error: function(errorMag) {
			alert("加载失败");
			myChart.hideLoading();
		}
	});
})();


// 跳转到页面2
(function() {
	midInput = document.getElementById("midInput");
	midBtn = document.getElementById("midBtn");
	
	midBtn.onclick = function(){
		console.log(midInput.value);
		if (midInput.value == ""){
			alert("请输入要爬取的视频bv号");
		}else{
			window.open("./select.html?bvid="+midInput.value);
		}
		
	}
	
	// $.ajax({
	// 	contentType: "application/json;charset=utf-8",
	// 	type: "put",
	// 	url: "http://localhost:8181/hotList/getAllGameList",
	// 	data: JSON.stringify(myUser),
	// 	dataType: "json",
	// 	success: function(res) {
	// 		if (res) {
	// 			// console.log(res);
	// 			for (var i in res) {
	// 				pics[i] = res[i].pic;
	// 				titles[i] = res[i].title;
	// 				if (i == 49) {
	// 					break;
	// 				}
	// 			}

	// 			var btn4 = document.getElementById("btn4");
	// 			btn4.onclick = function() {
	// 				var llist = document.getElementById("llist");
	// 				var lis = llist.getElementsByTagName("li");
	// 				console.log(pics[10]);

	// 				for (var i in lis) {
	// 					var img = lis[i].getElementsByClassName("img_pic")[0];
	// 					i = parseInt(i);
	// 					img.setAttribute("src", pics[i]);
	// 					var span = lis[i].getElementsByTagName("span")[0];
	// 					span.innerHTML = titles[i];
	// 					if (i == 9) break;
	// 				}
	// 			}
	// 		}
	// 	},
	// 	error: function(errorMag) {
	// 		alert("加载失败");
	// 		myChart.hideLoading();
	// 	}
	// });
})();
